package com.mooc.lib_user.ui.login

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.mooc.lib_common.CommonApp
import com.mooc.lib_common.bus.LiveDataBus
import com.mooc.lib_network.entity.NetResult
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class LoginViewModel :ViewModel(){
    val repo = LoginRepo()
    val loginBus = LiveDataBus<String>()

    fun login(avatar : String,description : String,expires_time : Long,name : String,qqOpenId : String){
        viewModelScope.launch(Dispatchers.IO){
            val result = repo.login(avatar,description,expires_time,0,0,name,qqOpenId,0)
            if (result is NetResult.Success){
                val entity = result.data.data
                entity!!.description = "默认值"
                CommonApp.db.userDao().insertUser(entity!!)
                CommonApp.userEntity = entity
                loginBus.postValue(""+System.currentTimeMillis())
            }
        }
    }

}